package top.hidoctor.auscultation.feature.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.NewsAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.NewsBean;
import top.hidoctor.auscultation.net.c.NewsContract;
import top.hidoctor.auscultation.net.p.NewsListPresenter;
import top.hidoctor.auscultation.net.req.PageReq;
import top.hidoctor.auscultation.widget.Player;

public class NewsActivity extends BaseActivity implements NewsContract.View {
    private RecyclerView recycler;
    private NewsAdapter adapter;
    private SwipeRefreshLayout refreshLayout;
    private NewsListPresenter presenter;
    private PageReq req;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_news);
        presenter = new NewsListPresenter(this, this);
        req = new PageReq();
        initView();
    }


    private void initView() {

        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("消息中心");
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        recycler = findViewById(R.id.recycler);
        refreshLayout = findViewById(R.id.refreshLayout);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter = new NewsAdapter(R.layout.item_news, null));
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        View view = LayoutInflater.from(this).inflate(R.layout.layout_empty_list, (ViewGroup) recycler.getParent(), false);
        adapter.setEmptyView(view);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        refresh();
    }


    private void refresh() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        presenter.getList(refreshLayout);
    }

    @Override
    public void getListSuc(List<NewsBean> bean) {
        adapter.setNewData(bean);
    }

    @Override
    public void onError(String code, String msg) {
        super.onError(code, msg);
        showToast(msg);
        refreshLayout.setRefreshing(false);
    }
}
